﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SharingCenter.Web.Classes;
using SharingCenter.Web.Classes.SiteEvents;

namespace SharingCenter.Web.Shared
{
    public partial class SiteEventList : System.Web.UI.UserControl
    {
        string appName = string.Empty;
        string eventName = string.Empty;
        bool publicOnly = true;
        Guid? raiserUserId = null;
        Guid? groupId = null;
        int daySpan = -1;
        Guid? currentUserId = null;
        string cssClass = string.Empty;
        int limitCount = 40;

        public string AppName { get { return appName; } set { appName = value; } }
        public string EventName { get { return eventName; } set { eventName = value; } }
        public bool PublicOnly { get { return publicOnly; } set { publicOnly = value; } }
        public Guid? GroupId { get { return groupId; } set { groupId = value; } }
        public Guid? RaiserUserId { get { return raiserUserId; } set { raiserUserId = value;  } }
        public int DaySpan { get { return daySpan; } set { daySpan = value; } }
        public Guid? CurrentUserId { get { return currentUserId; } set { currentUserId = value; } }
        public string CssClass { get { return cssClass; } set { cssClass = value; } }
        public int LimitCount { get { return limitCount; } set { limitCount = value; } }

        protected void Page_Init(object sender, EventArgs e)
        {
            ListView1.ItemDataBound += new EventHandler<ListViewItemEventArgs>(ListView1_ItemDataBound);
        }

        void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            Sc_SiteEvent evt = (Sc_SiteEvent)((ListViewDataItem)e.Item).DataItem;
            ((Literal)e.Item.FindControl("ltrTransformedEvent")).Text = SiteEventOperations.TransformTemplate(ref evt, Page);
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            Panel1.CssClass = CssClass;
            BindEventListView();
        }

        void BindEventListView()
        {
            List<Sc_SiteEvent> list = SiteEventOperations.GetEvents(raiserUserId, appName, eventName, publicOnly, daySpan, groupId, currentUserId, limitCount);
            ListView1.DataSource = list;
            ListView1.DataBind();
        }
    }
}